<template>
	<view class="mine-info">
		<navbar headTitle="志愿先进工作个人申请"></navbar>
		<view class="main">
			<!-- 姓名 -->
			<view class="submit-type">
				<view class="submit-type-title">
					<text>姓名</text>
				</view>
				<view class="submit-type-content">
					<view class="submit-input">
						<u-input v-model="userName" type="text" placeholder="请输入姓名" :clearable="false" :border="false" />
					</view>
				</view>
			</view>
			
			<!-- 性别 -->
			<view class="submit-type">
				<view class="submit-type-title">
					<text>性别</text>
				</view>
				<view class="submit-type-content">
					<view class="submit-radio">
						<view @click="sex='1'">
							<image style="width: 36rpx;" mode="widthFix" :src="sex=='2'?'../../static/common/radio-noselect.png':'../../static/common/radio-select.png'">
							</image>
							<text>男</text>
						</view>
						<view @click="sex='2'">
							<image style="width: 36rpx;" mode="widthFix" :src="sex=='1'?'../../static/common/radio-noselect.png':'../../static/common/radio-select.png'">
							<text>女</text>
						</view>
					</view>
				</view>
			</view>
			
			<!-- 出生日期 -->
			<view class="submit-type">
				<view class="submit-type-title">
					<text>出生日期</text>
				</view>
				<view class="submit-type-content">
					<uni-datetime-picker v-model="birthday" style="width: 80%"/>
				</view>
			</view>
			
			<!-- 政治面貌 -->
			<view class="submit-type">
				<view class="submit-type-title">
					<text>政治面貌</text>
				</view>
				<view class="submit-type-content">
					<view class="submit-input">
						<u-input v-model="politicalOutlook" type="text" placeholder="请输入政治面貌" :clearable="false" :border="false" />
					</view>
				</view>
			</view>
			
			<!-- 志愿者工作照片 -->
			<view class="submit-type">
				<view class="submit-type-title">
					<text>志愿者工作照片</text>
				</view>
				<view class="submit-type-content">
					<u-upload :form-data="formData" accept="image" @on-success="addPic" max-count="1" :action="action" :file-list="imageList" ></u-upload>
				</view>
			</view>
			
			<!-- 志愿者个人事迹 -->
			<view class="submit-type">
				<view class="submit-type-title">
					<text>志愿者个人事迹</text>
				</view>
				<view class="submit-type-content">
					<view class="submit-input">
						<u-input v-model="personalDeeds" maxlength="200" type="textarea" placeholder="请输入志愿者个人事迹" :clearable="false" :border="false" />
					</view>
				</view>
			</view>
			
			<view class="att-info">
				<text>注意：</text>
				<text>1、请核实信息的时效与准确</text>
				<text>2、有最新消息请及时反馈</text>
				<text>3、信息不全，影响证书发放</text>
			</view>
			<view class="release-btn" @click="submit()">确定</view>
		</view>
	</view>
</template>

<script>
	import uFilePicker from "../../components/uni-file-picker_0.2.9/components/uni-file-picker/uni-file-picker.vue"
	
	import moment from "moment";
	
	import {addAdvanced,queryByOpenIdAdvanced,editAdvanced} from "@/api/tab3.js";
	
	var _this;
	export default {
		data() {
			return {
				isEdit: false,
				
				baseUrl: "http://weixiaokong.91xiaokong.com/chewei/",
				
				/* 上传图片变量 */
				action: 'http://weixiaokong.91xiaokong.com/jeecg-boot/sys/common/app/upload',
				formData: {
					isup:1,
					bizPath: 'volunteer'  //剩下的字段
				},
				
				id: "",
				openid: "",
				sex: "2",
				userName: "",
				workPic: '',
				politicalOutlook:"",
				personalDeeds:"",
				birthday:"",
				// 上传图片
				imageList: [],
				
				url: ""
			}
		},
		components:{
			uFilePicker,
		},
		
		onLoad() {
			let _this = this;
			
			let openid = uni.getStorageSync('openid');
			if (openid) {
				_this.openId = openid;
			}
			
			_this.getopenId();
			/* 上传志愿者现金工作个人申请获取详情 */
			_this.getQueryByOpenIdAdvanced();
		},
		onShow() {
			
		},
		onReady() {

		},
		methods: {
			/* 上传志愿者现金工作个人申请获取详情 */
			getQueryByOpenIdAdvanced(){
				let self = this;
				queryByOpenIdAdvanced({openId: self.openId}).then((res)=>{
					self.id = res.result.id;
					self.sex = res.result.sex;
					self.userName = res.result.userName;
					self.workPic = res.result.workPic;
					self.politicalOutlook = res.result.politicalOutlook;
					self.personalDeeds = res.result.personalDeeds;
					self.birthday = res.result.birthday;
					self.imageList = [{
						url: self.workPic
					}];
					
					self.isEdit = true;
				})
			},
			
			/* 上传志愿者现金工作个人申请提交 */
			submit() {
				let self = this;
				if (
					self.userName=="" ||
					self.sex=="" ||
					self.personalDeeds==""
				) {
					uni.showToast({
						title: "请认真填写信息",
						icon: "error"
					});
					return;
				}
				// if(_this.aa.length != 1){
				// 		uni.showToast({
				// 			title: "请上传1张照片",
				// 			icon:"none"
				// 		});
				// 		return; 
				// 	}
				const openId = uni.getStorageSync('openid') || 'ogiDYs4eDr9DA2pofZeYsFQygtt4';
				let opt = {
					userName: self.userName,
					sex:self.sex,
					birthday:self.birthday,
					politicalOutlook: self.politicalOutlook,
					workPic: self.workPic,
					personalDeeds: self.personalDeeds,
					openId,
					id: self.id
				}
				let queryUrl;
				if(self.isEdit){
					queryUrl = editAdvanced;
				} else {
					queryUrl = addAdvanced;
				}
				queryUrl(opt).then((res)=>{
					console.log(res)
					if(res.code == 200){
						if(openId){
							uni.showToast({
								title: self.isEdit?"申请提交成功":"申请编辑成功",
								icon: "success"
							});
							setTimeout(()=>{
								uni.navigateBack({
									delta:1
								})
							},1500);
						}else{
							uni.showToast({
								title: self.isEdit?"申请提交失败":"申请编辑失败",
								icon: "error"
							});
						}
					}
				})
			},
			
			withData(param){
				return param < 10 ? '0' + param : '' + param;
			},
			change(e){
				let value = []
				e.detail.value.forEach((val,index) => {
					value.push(this.withData(val))
				})
				let dateArray = "20" + value[0] + "-" + value[1] + "-" + value[2] + " " + value[3] + ":" + value[4]
				this.upTower = dateArray
			},
			
			/* 上传图片 */
			addPic(data, index, lists, name){
				let self = this;
				console.log(data, index, lists, name);
				self.workPic = `https://weixiaokong.91xiaokong.com/${data.message}`;
            },
            
			// 获取openid
			getopenId() {
				let url = window.location.href;
				/* _this.url = url; */
				let url_1 = url.split('?');
				if (url_1.length > 1) {
					let url_2 = url_1[1].split('&')
					url_2.forEach(function(e) {
						if (e.indexOf('openid') > -1) {
							_this.openId = e.split('=')[1];
							uni.setStorage({
								key: 'openid',
								data: _this.openId,
								success: function() {
									console.log('success');
								}
							});
						}
						if (e.indexOf('nickname') > -1) {
							_this.nickname = e.split('=')[1];
							uni.setStorage({
								key: 'nickname',
								data: _this.nickname,
								success: function() {
									console.log('success');
								}
							});
						}
						if (e.indexOf('headimgurl') > -1) {
							_this.headimgurl = e.split('=')[1];
							uni.setStorage({
								key: 'headimgurl',
								data: _this.headimgurl,
								success: function() {
									console.log('success');
								}
							});
						}
					})
				}
			},
		}
	}
</script>

<style scoped lang="scss">
	.u-list-item {
		background: #f2f2f2;
		margin-bottom: 0;
	}
	
	.release-btn {
		width: 100%;
		height: 90rpx;
		margin: 90rpx auto;
		border-radius: 50rpx;
		text-align: center;
		line-height: 90rpx;
		background-color: #b30002;
		color: #ffffff;
		font-size: 32rpx;
	}
	
	.att-info {
		margin: 30rpx 0 0 10rpx;
		font-size: 30rpx;
		color: #b30002;
	}
	
	.mine-info {
		width: 100%;
		height: 100vh;
	}

	.main {
		padding: 0 30rpx;
		padding-top: 40rpx;
		width: 100%;
		background-color: #FFFFFF;
		height: calc(100% - 88rpx);
		overflow-y: scroll;
		box-sizing: border-box;
		
		.submit-type {
			width: 100%;
			padding-top: 30rpx;
			
			.submit-type-title {
				width: 100%;
				font-size: 32rpx;
				line-height: 32rpx;
				font-weight: bold;
				color: #333333;
				display: flex;
				justify-content: space-between;
				align-items: center;
			}
			
			.submit-type-content {
				width: 100%;
				margin-top: 25rpx;
				
				/* 单选框 */
				.submit-radio {
					width: 100%;
					height: 99rpx;
					display: flex;
					justify-content: space-around;
					align-items: center;
					color: #333333;
					font-size: 30rpx;
					
					view {
						display: flex;
						justify-content: space-between;
						align-items: center;
						
						text {
							margin-left: 12rpx;
						}
					}
				}
				
				/* 输入框 */
				.submit-input {
					background-color: #f2f2f2;
					width: 100%;
					padding-left: 30rpx;
					padding-top: 10rpx;
					padding-bottom: 10rpx;
					box-sizing: border-box;
					border-radius: 16rpx;
					overflow: hidden;
				}
			}
		}
	}

	.preview_img{
		position: relative;
		margin-left: 10px;
	}

	.uni-upload-img{
		display: flex;
	}

	.img_icon{
		background: rgba(0,0,0,.6);
		color: #fff;
		border-radius: 50%;
		position: absolute;
		right: -5px;
		top: -5px;
		display: inline-block;
		height: 20px;
		width: 20px;
		text-align: center;
		line-height: 20px;
	}
</style>
